package com.ogawa.projectcommon.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.ogawa.projectcommon.R
import kotlinx.android.synthetic.main.layout_time_range.view.*

@SuppressLint("SetTextI18n")
class TimeRangeView(context: Context, attributeSet: AttributeSet) :
    FrameLayout(context, attributeSet), View.OnClickListener {
    private val times = arrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
    private var startIndex = 0
    private var endIndex = startIndex + 1
    private val timeRanges = ArrayList<TimeRange>()

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_time_range, this)
        iv_start_up.setOnClickListener(this)
        iv_start_down.setOnClickListener(this)
        iv_end_up.setOnClickListener(this)
        iv_end_down.setOnClickListener(this)
        tv_start_time.text = "0${times[startIndex]}"
        tv_end_time.text = "0${times[endIndex]}"
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.iv_start_up -> {
                if (startIndex < 14) {
                    startIndex++
                }
                setStartTime()
            }
            R.id.iv_start_down -> {
                if (startIndex > 0) {
                    startIndex--
                }
                setStartTime()
            }
            R.id.iv_end_up -> {
                if (endIndex < 15) {
                    endIndex++
                }
                setEndTime()
            }
            R.id.iv_end_down -> {
                if (endIndex > 1) {
                    endIndex--
                }
                setEndTime()
            }
        }
    }

    private fun setStartTime() {
        if (times[startIndex] < 10) {
            tv_start_time.text = "0${times[startIndex]}"
        } else {
            tv_start_time.text = "${times[startIndex]}"
        }
        if (endIndex <= startIndex) {
            endIndex = startIndex + 1;
            setEndTime()
        }
    }

    private fun setEndTime() {
        tv_end_time.text = "0${times[endIndex]}"
        if (times[endIndex] < 10) {
            tv_end_time.text = "0${times[endIndex]}"
        } else {
            tv_end_time.text = "${times[endIndex]}"
        }
        if (startIndex >= endIndex) {
            startIndex = endIndex - 1;
            setStartTime()
        }
    }

    fun setTime(start: Int, end: Int) {
        startIndex = start
        endIndex = end
        setStartTime()
        setEndTime()
    }

    fun getStartTime(): Int {
        return times[startIndex]
    }

    fun getEndTime(): Int {
        return times[endIndex]
    }

    fun isTimeLegal(isHeat: Boolean = false): Boolean {
        val startTime = getStartTime()
        val endTime = getEndTime()
        if (isHeat) {
            if (endTime - startTime < 3) {
                return false
            }
        } else {
            if (startTime >= endTime) {
                return false
            }
        }
        for (timeRange in timeRanges) {
            if ((startTime >= timeRange.start && startTime < timeRange.end)
                || (endTime > timeRange.start && endTime <= timeRange.end)
                || (startTime < timeRange.start && endTime > timeRange.end)
            ) {
                return false
            }
        }
        return true
    }

    fun clearTimeRange() {
        timeRanges.clear()
    }

    fun setTimeRange(timeRange: TimeRange) {
        if (!timeRanges.contains(timeRange)) {
            timeRanges.add(timeRange)
        }
    }
}

data class TimeRange(val start: Int, val end: Int)
